<?php
// +----------------------------------------------------------------------
// | sfadmin - Suifeng Administration
// +----------------------------------------------------------------------
// | Copyright (c) 2017-2020 http://suifeng.tech All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: suifeng <1787585212@qq.com>
// +----------------------------------------------------------------------

namespace app\admin\controller;

use app\admin\common\AdminController;

class Config extends AdminController
{
    protected $reserve_params = array('group');

    public function index($group = 1)
    {
        $pagination = $this->_get_pagination_data();
        $pagination['where_map']['group'] = array('eq', $group);

        $pagination['rows_count'] = model('admin/config')
            ->where($pagination['where_map'])->count();

        $pagination['total_pages'] = ceil($pagination['rows_count'] / $pagination['page_size']);
        $pagination['page_num'] = $pagination['page_num'] > $pagination['total_pages'] ? 1 : $pagination['page_num'];

        $config_list = model('admin/config')
            ->where($pagination['where_map'])
            ->page($pagination['page_num'], $pagination['page_size'])
            ->order($pagination['sort_field'], $pagination['sort_order'])
            ->select();


        $this->assign('group', $group);
        $this->assign('config_list', $config_list);
        $this->assign('pagination', $pagination);
        $this->display('config/config_list');
    }

    public function create()
    {
        $config_info = model('admin/config')->initDefault();

        if (request()->isPost()) {
            $config_info->acceptData($this->post_array());

            if ($config_info->save() !== false) {
                $this->success('新增成功。', $this->url('index'));
            }
        }

        $this->assign('config_info', $config_info);
        $this->display('config/config_form');
    }

    public function modify($id)
    {
        $config_info = model('admin/config')->findOrFail($id);

        if (request()->isPost()) {
            $config_info->acceptData($this->post_array());

            if ($config_info->save() !== false) {
                $this->success('修改成功。', $this->url('index'));
            }
        }

        $this->assign('config_info', $config_info);
        $this->display('config/config_form');
    }

    public function bulk_delete($ids = array())
    {
        $config_list = model('admin/config')->where('id', 'in', $ids)->select();

        foreach ($config_list as $config_info) {
            if ($config_info['is_system']) {
                $this->error('系统选项不允许删除。');
            }
        }

        foreach ($config_list as $config_info) {
            $config_info->delete();
        }

        $this->success('删除成功。', $this->url('index'));
    }

    public function settings($group = 1)
    {
        if (request()->isPost()) {
            $config_array = (array) $this->post_value('config');
            foreach ($config_array as $name => $value) {
                $config_info = model('admin/config')
                    ->where('name', $name)
                    ->findOrFail();
                $config_info['value'] = $value;
                $config_info->save();
            }

            $this->success('修改成功。', $this->url('settings'));
        }

        $where_map['status'] = array('eq', 1);
        $where_map['group'] = array('eq', $group);

        $config_list = model('admin/config')
            ->where($where_map)
            ->order('sort asc, id asc')
            ->select();

        foreach ($config_list as &$config_info) {
            if ($config_info['options']) {
                $config_info['options'] = helper('admin/options')->parse($config_info['options']);
            }
        }

        $this->assign('config_list', $config_list);
        $this->assign('group', $group);
        $this->display('config/config_settings');
    }
}
